<?php
 
/* Common FN */
function check_is_https() {
    if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER['HTTPS']) != 'off') {
        return true;
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) == 'https') {
        return true;
    } elseif (isset($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) != 'off') {
        return true;
    } else {
        return false;
    }
}
 
function get_url_protocol_and_host() {
    $p = check_is_https() ? 'https://' : 'http://';
    return $p . $_SERVER['HTTP_HOST'] . '/';
}
 
/**
 * get_list_of_folders_by_path_not_recursive
 * 
 * @param string $dir
 * @param type $sort
 * @param type $except_hidden
 * @return type
 */
function get_list_of_folders_by_path_not_recursive ($dir, $sort = 'ASC', $except_hidden = true) {
    $return = array();
    $dir = rtrim($dir, '/') . '/';
    if (is_dir($dir)) {
        $dh = opendir($dir);
 
        while (($file = readdir($dh)) !== false) {
            if (!in_array($file, array('.', '..'))) {
                if (is_dir($dir . $file)) {
                    if(!$except_hidden){
                        $return[] = $file;
                    } elseif($except_hidden && preg_match('/^(?!\.)/', $file)) {
                        $return[] = $file;
                    } else {
 
                    }
                }
            }
        }
        closedir($dh);
    }
 
    if ($sort == 'DESC') {
        arsort($return);
        $return = array_values($return);
    }
    if ($sort == 'ASC') {
        asort($return);
        $return = array_values($return);
    }
 
    return $return;
} 
 
/**
 * get_list_of_files_by_path_not_recursive
 * 
 * @param string $dir
 * @param type $sort
 * @param type $except_hidden
 * @return type
 */
function get_list_of_files_by_path_not_recursive($dir, $sort = 'ASC', $except_hidden = true) {
    $return = array();
    $dir = rtrim($dir, '/') . '/';
    if (is_dir($dir)) {
        $dh = opendir($dir);
 
        while (($file = readdir($dh)) !== false) {
            if (!in_array($file, array('.', '..'))) {
                if (is_file($dir . $file)) {
                    if(!$except_hidden){
                        $return[] = $file;
                    } elseif($except_hidden && preg_match('/^(?!\.)/', $file)) {
                        $return[] = $file;
                    } else {
 
                    }
                }
            }
        }
        closedir($dh);
    }
 
    if ($sort == 'DESC') {
        arsort($return);
        $return = array_values($return);
    }
    if ($sort == 'ASC') {
        asort($return);
        $return = array_values($return);
    }
 
    return $return;
}
 
 
 
 
/**
 * PHP - 遍历获取目录下所有的文件列表 - 无限递归
 * @author Rudon <285744011@qq.com>
 * @link https://www.php.net/manual/zh/class.recursiveiteratoriterator.php 递归迭代器
 * 
 * @param string $dirPath 目录路径
 * @param boolean $withDirectory 是否需要子目录对象
 * @return array 文件列表
 */
function get_files_recursive_in_folder ($dirPath, $withDirectory = false) {
    // 返回列表
    $list = [];
 
    // 路径分隔符
    $ds = DIRECTORY_SEPARATOR;
 
    // 目录路径去除右边的分隔符
    $dirPath = rtrim(rtrim($dirPath, '/'), '\\');
 
    // 非法目录直接跳过
    if (!is_dir($dirPath)) {
        return $list;
    }
 
    // 递归迭代器
    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dirPath),
        RecursiveIteratorIterator::LEAVES_ONLY
    );
    foreach ($iterator as $itemPath => $file)
    {
        // 是否目录
        $isDir = boolval($file->isDir());
        
        // 绝对路径
        $realPath = $file->getRealPath();
        
        // 文件名称或目录名称
        $name = $file->getFilename();
        $dirsArr = explode($ds, rtrim($realPath, $ds));
        $nameDirOrFile = ($isDir)?array_pop($dirsArr):$name;
 
        // 从入参目录开始的相对目录
        $path_sub = ltrim(str_ireplace($dirPath, '', $realPath), $ds);
        if ($isDir) { $path_sub .= $ds; }
 
        // 不希望返回目录对象
        if ($isDir && !$withDirectory) {  continue; }
 
        // 上级目录“..”跳过，防止重复
        if ($isDir && $name === '..') { continue; }
 
        // 跳过入参目录
        if ($realPath == $dirPath) { continue; }
 
        // 目标对象
        $oneTarget = [
            'is_dir' => $isDir,
            'name' => $nameDirOrFile,
            'path' => $realPath,
            'path_sub' => $path_sub
        ];
        $list[] = $oneTarget;
    }
    return $list;
}
 
// 递归删除目录 - 删除文件夹
function del_target_dir($path, $delDir = true)
{
    //没找到，不处理
    if (!file_exists($path)) {
        return false;
    }
 
    //打开目录句柄
    $handle = opendir($path);
    if ($handle) {
        while (false !== ($item = readdir($handle))) {
            if ($item != "." && $item != "..") {
                if (is_dir("$path/$item")) {
                    del_target_dir("$path/$item", $delDir);
                } else {
                    unlink("$path/$item");
                }
            }
        }
        closedir($handle);
        if ($delDir) {
            return rmdir($path);
        }
    } else {
        if (file_exists($path)) {
            return unlink($path);
        }
        return false;
    }
}
 
function a ($v) {
    header('Content-Type: text/css; charset=utf-8');
    print_r($v);
    die();
}
 
function make_sure_dir_exists ($dir, $mode = 0755) {
    if (is_dir($dir)) {
        return;
    }
    mkdir($dir, $mode, true);
}
 
function make_sure_file_exists ($file, $content = '') {
    if (file_exists($file)) {
        return;
    }
    if (is_dir($file)) {
        return;
    }
    $dir = dirname($file);
    make_sure_dir_exists($dir);
    file_put_contents($file, $content);
}
 
 
?>